package com.frontend.api.controller;

import com.common.dto.CategoryDTO;
import com.common.exception.ModelNotFoundException;
import com.common.service.CategoryService;
import com.util.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "CategoryController", value = "分类接口")
public class CategoryController {

    @Resource
    CategoryService categoryService;

    @ApiOperation("获取分类列表")
    @RequestMapping(value = "/category", method = RequestMethod.GET)
    public CommonResult<List<CategoryDTO>> getCategory() {
        List<CategoryDTO> categories = categoryService.selectAllCategory();
        return CommonResult.success(categories);
    }

    @ApiOperation("分类信息")
    @RequestMapping(value = "/category/{id}", method = RequestMethod.GET)
    public CommonResult<CategoryDTO> getSingleCategory(
            @PathVariable("id") Long id
    ) {
        try {
            CategoryDTO category = categoryService.selectById(id);
            return CommonResult.success(category);
        } catch (ModelNotFoundException e) {
            return CommonResult.failed("这个分类已经去火星了QAQ");
        }
    }


}
